/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.weijiwufang.ui.action;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import org.dusin.weijiwufang.bo.StatusBean;
import org.dusin.weijiwufang.service.StatusService;
import org.dusin.wufang.util.WuFangUtil;

/**
 *
 * @author dusin
 */
public class StatusManageAction implements ActionListener {

    private static final String title = "运行状态管理";
    private JFrame mainFrame;  //当前面板
    private JTabbedPane pane;
    StatusService service;
    JTable statusTable;

    public StatusManageAction(JFrame mainFrame,JTabbedPane tabbedPane, StatusService service) {
        this.mainFrame = mainFrame;
        this.pane = tabbedPane;
        this.service = service;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        int i = pane.indexOfTab(title);
        if (i == -1) {
            pane.addTab(title, null, makePanel(),
                    title);
            i = pane.getTabCount() - 1;
            pane.setTabComponentAt(i,
                    new ButtonTabComponent(pane, "/org/dusin/weijiwufang/image/group.png"));
        }
        pane.setSelectedIndex(i);

    }

    protected JComponent makePanel() {
        JPanel panel = new JPanel(false);
        panel.setLayout(new BorderLayout());

        JToolBar toolBar = new JToolBar("Still draggable");

        JButton addButton = new JButton("增加", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/status_add.png"));
        addButton.addActionListener(this.new AddStatusAction());

        JButton updateButton = new JButton("修改", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/status_edit.png"));
        updateButton.addActionListener(this.new UpdateStatusAction());

        JButton delButton = new JButton("删除", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/status_delete.png"));
        delButton.addActionListener(this.new DelStatusAction());


        toolBar.add(addButton);
        toolBar.add(updateButton);
        toolBar.add(delButton);
        toolBar.add(new JSeparator(SwingConstants.VERTICAL));

        //addButtons(toolBar);
        panel.add(toolBar, BorderLayout.NORTH);

        JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        statusTable = new javax.swing.JTable();
        statusTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null}
                },
                new String[]{
                    "id", "状态名称", "备注"
                }) {
            Class[] types = new Class[]{
                java.lang.Long.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        statusTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(statusTable);
        panel.add(jScrollPane1, BorderLayout.CENTER);
        loadData();        
        return panel;
    }

    public void loadData() {
        DefaultTableModel tableModel = (DefaultTableModel) statusTable.getModel();
        tableModel.setRowCount(0);
        List<StatusBean> list = null;
        try {
            list = service.findAll();
            for (StatusBean v : list) {
                tableModel.addRow(new Object[]{v.getId(), v.getName(), v.getNote()});
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    class AddStatusAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            StatusInfoDialog StatusInfoDialog = new StatusInfoDialog(mainFrame, true);
            //statusInfoDialog.set
            //mainFrame.set
            StatusInfoDialog.setLocationRelativeTo(mainFrame);
            StatusInfoDialog.setTitle("增加状态信息");
            StatusInfoDialog.setVisible(true);
            StatusBean status;
            status = StatusInfoDialog.getStatus();
            if (status != null) {
                service.addStatus(status);
                loadData();
            }
            //statusInfoDialog.setVisible(false);
            StatusInfoDialog.dispose();

        }
    }

    class UpdateStatusAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {

            StatusBean status = new StatusBean();
            StatusInfoDialog statusInfoDialog = new StatusInfoDialog(mainFrame, true);
            statusInfoDialog.setLocationRelativeTo(mainFrame);
            statusInfoDialog.setTitle("修改状态信息");
            int selectedRowIndex = statusTable.getSelectedRow();
            if (selectedRowIndex != -1) {

                DefaultTableModel tableModel = (DefaultTableModel) statusTable.getModel();
                long currentId = (Long) (tableModel.getValueAt(selectedRowIndex, 0));                
                
                String name = (String) (tableModel.getValueAt(selectedRowIndex, 1));
                status.setName(name);
                
                String note = (String) (tableModel.getValueAt(selectedRowIndex, 2));
                status.setNote(note);
                
                
                statusInfoDialog.setStatus(status);

                statusInfoDialog.setVisible(true);

                status = statusInfoDialog.getStatus();
                status.setId(currentId);
                if (status != null) {
                    service.updateStatus(status);
                }

                statusInfoDialog.dispose();
                loadData();
            } else {
                JOptionPane.showMessageDialog(null, "请选择一项");
            }

        }
    }

    class DelStatusAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            int selectedRowIndex = statusTable.getSelectedRow();
            if (selectedRowIndex != -1) {
                if (JOptionPane.showConfirmDialog(null, "你确定删除吗？") == JOptionPane.OK_OPTION) {
                    DefaultTableModel tableModel = (DefaultTableModel) statusTable.getModel();
                    long currentId = (Long) (tableModel.getValueAt(selectedRowIndex, 0));                    
                    service.deleteStatus(currentId);
                    loadData();
                }

            } else {
                JOptionPane.showMessageDialog(null, "请选择一项删除");
            }

        }
    }
}
